﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.NewObject"/>.
    /// </summary>
    public class NewObjectVM : NewOperatorVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(NewObject),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new NewObjectVM((NewObject)codeObject, dictionary); });
        }

        #endregion

        #region /* FIELDS */

        protected SymbolicRefVM _constructorRefVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.NewObject"/>.
        /// </summary>
        public NewObjectVM(NewObject newObject, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(newObject, dictionary)
        {
            _constructorRefVM = (SymbolicRefVM)CreateVM(newObject.HiddenRef, false, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.NewObject"/> model.
        /// </summary>
        public NewObject NewObject
        {
            get { return (NewObject)CodeObject; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        /// <summary>
        /// Determines if empty parens should be hidden.
        /// </summary>
        public static bool HideEmptyParens;

        protected override void RenderName(CodeRenderer renderer, RenderFlags flags)
        {
            renderer.RenderText(NewOperator.ParseToken, _constructorRefVM != null ? _constructorRefVM.GetMessageBrush(KEYWORD_BRUSH, flags) : KEYWORD_BRUSH, this);
            if (_expressionVM != null)
                _expressionVM.Render(renderer, flags | RenderFlags.PrefixSpace);
        }

        public override void RenderExpression(CodeRenderer renderer, RenderFlags flags)
        {
            // Suppress empty parens *if* we have an initializer.
            // Also suppress them if the special option is on and the parent isn't a Dot.
            base.RenderExpression(renderer, flags | (_initializerVM != null || (HideEmptyParens && !(ParentVM is DotVM)) ? RenderFlags.NoParensIfEmpty : 0));
        }

        protected override void RenderStartArguments(CodeRenderer renderer, Brush brush, RenderFlags flags)
        {
            renderer.RenderText(NewObject.ParseTokenStart, brush, this);
        }

        protected override void RenderEndArguments(CodeRenderer renderer, Brush brush, RenderFlags flags)
        {
            if (NewObject.IsEndFirstOnLine)
                renderer.NewLine();
            renderer.RenderText(NewObject.ParseTokenEnd, brush, this);
        }

        public override void UnRender()
        {
            if (_constructorRefVM != null)
                _constructorRefVM.UnRender();
            base.UnRender();
        }

        public override void RenderToolTip(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderToolTip(renderer, flags);
            if (_constructorRefVM != null)
            {
                renderer.NewLine();
                CreateVM(NewObject.HiddenRef).RenderToolTip(renderer, RenderFlags.SuppressLineCol);
            }
        }

        #endregion

        #region /* COMMANDS */

        public static readonly RoutedCommand HideEmptyParensCommand = new RoutedCommand("Hide Empty Parens on 'new Type()'", typeof(NewObjectVM));

        public static new void BindCommands(Window window)
        {
            WPFUtil.AddCommandBinding(window, HideEmptyParensCommand, hideEmptyParens_Executed);
        }

        private static void hideEmptyParens_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            HideEmptyParens = !HideEmptyParens;
            CodeRenderer.ReRenderAll();
        }

        #endregion
    }
}
